﻿using SalesSys.DAL.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.Comm.Utility;

namespace SalesSys.DAL.Models
{
 
    /// <summary>
    /// 计划单实体类
    /// </summary>
    public class Order : DbEntityBase<Guid>
    {

        public readonly static TimeNumBulider OrderNumBulider = new TimeNumBulider();

        public Order()
        {
            CodeNum = OrderNumBulider.NextNum();
        }

        public class Mapper : SalMapper<Order, Guid>
        {
            public Mapper()
            {
                HasMany(p => p.Records).WithOptional().WillCascadeOnDelete();
                HasMany(p => p.CommodityInfos).WithOptional().WillCascadeOnDelete();
                //HasMany(p => p.VehicleInfos).WithOptional().WillCascadeOnDelete();
            }
        }

        /// <summary>
        /// 订单编号
        /// </summary>
        public string CodeNum { get; set; }
       // public DeliverySheet DeliverySheet { get; set; }
        /// <summary>
        /// 创建人，一般为客户专员
        /// </summary>
        public virtual Employee Creater { get; set; }

        /// <summary>
        /// 客户
        /// </summary>
        public virtual Customer Customer { get; set; } 

        /// <summary>
        /// 支付户头
        /// </summary>
        public virtual Account Account { get; set; }

        /// <summary>
        /// 地区
        /// </summary>
        public virtual District District { get; set; }

        /// <summary>
        /// 详细地址
        /// </summary>
        public string Address { get; set; }
        #region 优惠
         /// <summary>
        /// 付款方式-是否先款
        /// </summary>
        public bool IsFullPayment { get; set; }

        /// <summary>
        /// 是否标件
        /// </summary>
        public bool IsStandard { get; set; }


        /// <summary>
        /// 先款每吨优惠金额,根据先款计算出平均优惠单价
        /// </summary>
        public decimal UnitDiscount { get; set; }

        /// <summary>
        /// 先款优惠合计,
        /// </summary>
        public decimal UnitTotal { get; set; }

        /// <summary>
        /// 申请单价优惠金额,
        /// </summary>
        public decimal ApplyDiscount { get; set; }

        /// <summary>
        /// 直接优惠合计，
        /// </summary>
        public decimal BargainingAmount { get; set; }
        /// <summary>
        /// 非标优惠
        /// </summary>
        public decimal? NonstandardDiscount { get; set; }

        #endregion
       

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 运费户头
        /// </summary>
        public virtual Account DeliveryAccount { get; set; }
        /// <summary>
        /// 已缴运费
        /// </summary>
        public decimal PaidDelivery { get; set; }


        /// <summary>
        /// 每吨运费
        /// </summary>
        public decimal DeliveryPrice { get; set; }
        /// <summary>
        /// 总运费，
        /// </summary>
        public decimal DeliveryPriceTotal { get; set; }

        /// <summary>
        /// 运费是否公户
        /// </summary>
        public bool IsPublicDelivery { get; set; }

        /// <summary>
        /// 是否送货：true=送货;false=自提
        /// </summary>
        public bool IsDelivery { get; set; }

        /// <summary>
        /// 联系人
        /// </summary>
        public string Contact { get; set; }

        /// <summary>
        /// 联系人手机号
        /// </summary>
        public string ContactPhone { get; set; }

        /// <summary>
        /// 车辆信息
        /// </summary>
        public string VehicleInfos { get; set; }
        /// <summary>
        /// 车辆信息
        /// </summary>
        //public virtual ICollection<VehicleInfo> VehicleInfos { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Remarks { get; set; }

        /// <summary>
        /// 仓库
        /// </summary>
        public virtual Provider Provider { get; set; }

        /// <summary>
        /// 关联锁价单编号
        /// </summary>
        public virtual LockOrder LockOrder { get; set; }

        /// <summary>
        /// 货品信息
        /// </summary>
        public virtual ICollection<CommodityInfo> CommodityInfos { get; set; }

        /// <summary>
        /// 订单类型
        /// </summary>
        public OrderType OrderType { get; set; }

        /// <summary>
        /// 实际重量
        /// </summary>
        [DecimalPrecision(18, 3)]
        public decimal Weight { get; set; }

        /// <summary>
        /// 实际价格
        /// </summary>
        public decimal Total { get; set; }
        /// <summary>
        /// 已付金额，已分配给改订单的预付款，主要用于判断是否可以先款优惠
        /// </summary>
        public decimal Prepayments { get; set; }

        /// <summary>
        /// 退款
        /// </summary>
        public decimal Refund { get; set; }

        /// <summary>
        /// 原重量
        /// </summary>
        [DecimalPrecision(18, 3)]
        public decimal SourceWeight { get; set; }

        /// <summary>
        /// 原价格
        /// </summary>
        public decimal SourceTotal { get; set; }

        /// <summary>
        /// 根据商品信息重新计算重量及价格,优惠也会计算在内
        /// </summary>
        public void CalcNew()
        {
            if (CommodityInfos == null)
            {
                Weight = 0;
                Total = 0;
                BargainingAmount = 0;
                UnitTotal = 0;
                SourceWeight = 0;
                SourceTotal = 0;
            }
            else
            {
                Weight = CommodityInfos.Sum(p => p.Weight);
                //Weight = decimal.Parse(Weight.ToString("f3"));
                //先款优惠合计
                UnitTotal = decimal.Parse((UnitDiscount * Weight).ToString("f2"));
                //议价优惠
                var applyDis = decimal.Parse((Weight * ApplyDiscount).ToString("f2"));
                var sum = decimal.Parse(CommodityInfos.Sum(p => p.Price * p.Weight).ToString("f2"));
                //sum = decimal.Parse(sum.ToString("f3"));
                Total = sum - applyDis - UnitTotal - BargainingAmount;
                if (SourceTotal == 0.0M)
                    SourceTotal = Total;
                if (SourceWeight == 0)
                    SourceWeight = Weight;
                //如果是锁价单创建的计划单，则自动更新已付金额为订单金额
                if (LockOrder != null)
                    this.Prepayments = this.Total;
            }
        }

        public void CalcDelivery()
        {
            DeliveryPriceTotal = Weight * DeliveryPrice;
        }

        /// <summary>
        /// 根据商品信息计算每吨优惠
        /// </summary>
        public void CalcUnitDiscount()
        {
            if (CommodityInfos == null || this.Provider == null)
            {
                UnitDiscount = 0;
            }
            else
            {
                if (this.IsFullPayment)
                    this.UnitDiscount += this.Provider.FullPaymentDiscount;
                if (!this.IsStandard)
                {
                    this.UnitDiscount += this.Provider.NonstandardDiscount;
                    this.NonstandardDiscount = this.Provider.NonstandardDiscount;
                }
            }
        }
        /// <summary>
        /// 是否已确定实重，生成出库单
        /// </summary>
        public bool IsToDeliveryList { get; set; } 
        /// <summary>
        /// 订单状态
        /// </summary>
        public OrderState State { get; set; }
        /// <summary>
        /// 是否已结算
        /// </summary>
        public bool IsSettlement { get; set; }
        /// <summary>
        /// 结算时间
        /// </summary>
        public DateTime? SettlementTime { get; set; }
        /// <summary>
        /// 是否审批中，审批流程中会包含某个计划单编号
        /// </summary>
        public bool IsApproval { get; set; }

        /// <summary>
        /// 是否暂停
        /// </summary>
        public bool IsPause { get; set; }
        
        /// <summary>
        /// 流程状态
        /// </summary>
        public ProcessState ProcessState { get; set; }
        /// <summary>
        /// 发货时间
        /// </summary>
        public DateTime? SendTime { get; set; }

        ///// <summary>
        ///// 是否议价
        ///// </summary>
        //public bool IsBargaining { get; set; }

        /// <summary>
        /// 是否锁价
        /// </summary>
        public bool IsLockPrice { get; set; }

        /// <summary>
        /// 订单变化记录
        /// </summary>
        public virtual ICollection<OrderRecord> Records { get; set; }
    }

}
